package com.wmyg.tracker.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter;
import org.springframework.amqp.rabbit.support.MessagePropertiesConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Autowired
    private RabbitProperties rabbitProperties;


//    @Bean("connectionFactory")
//    public ConnectionFactory getConnectionFactory() {
//        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory =
//                new com.rabbitmq.client.ConnectionFactory();
//        rabbitConnectionFactory.setHost(rabbitProperties.getHost());
//        rabbitConnectionFactory.setPort(rabbitProperties.getPort());
//        rabbitConnectionFactory.setUsername(rabbitProperties.getUsername());
//        rabbitConnectionFactory.setPassword(rabbitProperties.getPassword());
//
//        ConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitConnectionFactory);
//        return connectionFactory;
//
//    }

    @Bean(name="rabbitAdmin")
    public RabbitAdmin getRabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;

    }



    @Bean(name="serializerMessageConverter")
    public MessageConverter getMessageConverter(){
        return new SimpleMessageConverter();
    }

    @Bean(name="messagePropertiesConverter")
    public MessagePropertiesConverter getMessagePropertiesConverter()
    {
        return new DefaultMessagePropertiesConverter();
    }




    @Bean(name="rabbitTemplate")
    public RabbitTemplate getRabbitTemplate(ConnectionFactory connectionFactory)
    {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setUseTemporaryReplyQueues(false);
        rabbitTemplate.setMessageConverter(getMessageConverter());
        rabbitTemplate.setMessagePropertiesConverter(getMessagePropertiesConverter());

        return rabbitTemplate;
    }



    @Bean(name="fabricMessageQueue")
    public Queue createQueue(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin)
    {
        Queue sendQueue = new Queue(AppConstants.SEND_QUEUE_NAME,true,false,false);
//        rabbitAdmin.declareQueue(sendQueue);
        return sendQueue;
    }

    @Bean(name="fabricMessageExchange")
    public Exchange createExchange(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin)
    {
        DirectExchange sendExchange = new DirectExchange(AppConstants.SEND_EXCHANGE_NAME,true,false);
//        rabbitAdmin.declareExchange(sendExchange);
        return sendExchange;
    }

    @Bean(name="fabricMessageBinding")
    public Binding createMessageBinding(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin)
    {
        Map<String,Object> arguments = new HashMap<String,Object>();
        Binding sendMessageBinding =
                new Binding(AppConstants.SEND_QUEUE_NAME, Binding.DestinationType.QUEUE,
                        AppConstants.SEND_EXCHANGE_NAME, AppConstants.SEND_MESSAGE_KEY, arguments);
//        rabbitAdmin.declareBinding(sendMessageBinding);
        return sendMessageBinding;
    }

//    @Bean(name = "springReplyMessageQueue")
//    public Queue createSpringReplyMessageQueue(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin) {
//        Queue replyQueue = new Queue("springReplyMessageQueue", true, false, false);
//        rabbitAdmin.declareQueue(replyQueue);
//        return replyQueue;
//    }
//
//    @Bean(name="springReplyMessageExchange")
//    public Exchange createSpringReplyMessageExchange(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
//    {
//        DirectExchange replyExchange = new DirectExchange("springReplyMessageExchange",true,false);
//        rabbitAdmin.declareExchange(replyExchange);
//        return replyExchange;
//    }
//
//    @Bean(name="springReplyMessageBinding")
//    public Binding createSpringReplyMessageBinding(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
//    {
//        Map<String,Object> arguments = new HashMap<>();
//        Binding replyMessageBinding =
//                new Binding("springReplyMessageQueue", Binding.DestinationType.QUEUE,
//                        "springReplyMessageExchange", "springReplyMessage", arguments);
//        rabbitAdmin.declareBinding(replyMessageBinding);
//        return replyMessageBinding;
//    }

}
